package com.clarence.dada.core.factory;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.clarence.dada.core.auth.entity.LoginUser;
import com.clarence.dada.core.auth.entity.basic.SimpleRoleInfo;
import com.clarence.dada.core.auth.entity.basic.SimpleUserInfo;
import com.clarence.dada.core.auth.entity.dto.UserLoginInfoDto;
import com.clarence.dada.modular.system.role.entity.resp.SysRoleResp;
import com.clarence.dada.modular.system.user.entity.SysUserEntity;
import com.clarence.dada.modular.system.user.entity.resp.CurrentUserInfoResp;
import com.clarence.dada.modular.system.user.service.SysUserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 组装当前登录用户的信息
 *
 * @author fengshuonan
 * @date 2020/11/26 22:25
 */
public class UserLoginInfoFactory {

    /**
     * 组装登录用户信息对象
     *
     * @param sysUser                 用户信息
     * @param roleRespList            角色集合信息
     * @param resourceUrlsListByCodes 用户的所有资源url
     * @param roleButtonCodes         用户的所拥有的按钮编码
     * @author fengshuonan
     * @date 2020/12/26 17:53
     */
    public static UserLoginInfoDto userLoginInfoDTO(SysUserEntity sysUser,
                                                    List<SysRoleResp> roleRespList,
                                                    Set<String> resourceUrlsListByCodes,
                                                    Set<String> roleButtonCodes) {

        UserLoginInfoDto userLoginInfoDTO = new UserLoginInfoDto();

        // 设置用户加密的密码和状态
        userLoginInfoDTO.setUserPasswordHexed(sysUser.getPassword());
        userLoginInfoDTO.setUserStatus(sysUser.getStatusFlag());

        // 创建登录用户对象
        LoginUser loginUser = new LoginUser();

        // 填充用户账号，账号id，管理员类型
        loginUser.setAccount(sysUser.getAccount());
        loginUser.setUserId(sysUser.getUserId());
        loginUser.setSuperAdmin(sysUser.getUserId() == -1);

        // 填充用户基本信息
        SimpleUserInfo simpleUserInfo = new SimpleUserInfo();
        BeanUtil.copyProperties(sysUser, simpleUserInfo);
        loginUser.setSimpleUserInfo(simpleUserInfo);

        // 填充用户角色信息
        if (!roleRespList.isEmpty()) {
            ArrayList<SimpleRoleInfo> simpleRoleInfoList = new ArrayList<>();
            for (SysRoleResp sysRole : roleRespList) {
                SimpleRoleInfo simpleRoleInfo = new SimpleRoleInfo();
                BeanUtil.copyProperties(sysRole, simpleRoleInfo);
                simpleRoleInfoList.add(simpleRoleInfo);
            }
            loginUser.setSimpleRoleInfoList(simpleRoleInfoList);
        }

        // 设置用户拥有的资源
        loginUser.setResourceUrls(resourceUrlsListByCodes);

        // 填充用户拥有的按钮编码
        loginUser.setButtonCodes(roleButtonCodes);

        // 设置用户的登录时间

        userLoginInfoDTO.setLoginUser(loginUser);
        return userLoginInfoDTO;
    }

    /**
     * 转化为当前登陆用户信息的详情
     *
     * @author fengshuonan
     * @date 2021/3/25 10:06
     */
    public static CurrentUserInfoResp parseUserInfo(LoginUser loginUser) {

        SysUserService sysUserService = SpringUtil.getBean(SysUserService.class);
        CurrentUserInfoResp currentUserInfoResponse = new CurrentUserInfoResp();

        // 设置用户id
        currentUserInfoResponse.setUserId(loginUser.getUserId());

        // 设置用户昵称
        currentUserInfoResponse.setNickname(loginUser.getSimpleUserInfo().getNickName());

        // 姓名
        currentUserInfoResponse.setRealName(loginUser.getSimpleUserInfo().getRealName());

        // 设置头像，并获取头像的url
        Long avatarFileId = loginUser.getSimpleUserInfo().getAvatar();
        String userAvatarUrl = sysUserService.getUserAvatarUrl(avatarFileId, loginUser.getToken());
        currentUserInfoResponse.setAvatar(userAvatarUrl);

        // 设置角色
        List<SimpleRoleInfo> simpleRoleInfoList = loginUser.getSimpleRoleInfoList();
        Set<String> roleCodes = simpleRoleInfoList.stream().map(SimpleRoleInfo::getRoleCode).collect(Collectors.toSet());
        currentUserInfoResponse.setRoles(roleCodes);

        // 设置用户拥有的按钮权限
        currentUserInfoResponse.setAuthorities(loginUser.getButtonCodes());

        return currentUserInfoResponse;
    }

}
